﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using TidyBlog.MVC;

namespace TidyBlog.MVC.WWW
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        /// <summary>
        /// 重写规则
        /// </summary>
        protected static readonly Dictionary<string, Regex> RewriteRules = new Dictionary<string, Regex>();


        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var context = ((HttpApplication)sender).Context;
            string fileName = Path.GetFileName(context.Request.Path);

            foreach (var r in RewriteRules)
            {
                var match = r.Value.Match(fileName);
                if (match.Success)
                {
                    context.RewritePath(r.Value.Replace(fileName, r.Key));
                    break;
                }
            }
        }

        protected static void RegisterRewriteRules()
        {
            // 分类编号重写
            RewriteRules.Add(
                "~/", new Regex(@"^Default.aspx$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
            );
            // 分类编号重写
            RewriteRules.Add(
                "~/Article/Index/$1/$2", new Regex(@"^ShowIndex(\d+)(?:-(\d+))?\.aspx$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
            );
            // 文章编号重写
            RewriteRules.Add(
                "~/Article/Detail/$1", new Regex(@"^ShowArticle-?(\d+)\.aspx$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
            );
            // 文章附件路径重写
            RewriteRules.Add(
                "~/FileTransfer.ashx", new Regex(@"^FileTransfer\.ashx$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
            );
            // 文章RSS路径重写
            RewriteRules.Add(
                "~/Article/RSS", new Regex(@"^Rss\.ashx$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
            );
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "ArticleIndex",
                "Article/Index/{id}/{page}",
                new
                {
                    controller = "Article",
                    action = "Index",
                    id = "0",
                    page = "1"
                }
            );
            
            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Article", action = "Index", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            RegisterRewriteRules();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ModelBinders.Binders.Add(typeof(string), new StringTrimModelBinder());

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
        }
    }
}